package com.sl.biz.modular.job.provider;/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */

import cn.hutool.core.bean.BeanUtil;
import com.sl.biz.api.JobApi;
import com.sl.biz.domain.request.JobAddRpcRequest;
import com.sl.biz.domain.request.JobPageParamRpcRequest;
import com.sl.biz.domain.request.JobUpdateByIdRpcRequest;
import com.sl.biz.domain.response.JobVO;
import com.sl.biz.modular.job.controller.request.JobAddRequest;
import com.sl.biz.modular.job.controller.request.JobGetByIdRequest;
import com.sl.biz.modular.job.controller.request.JobUpdateByIdRequest;
import com.sl.biz.modular.job.dao.entity.JobConfigDO;
import com.sl.biz.modular.job.service.JobConfigBizService;
import com.sl.common.page.PageList;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 邮件API接口提供者
 *
 * @author xuyuxiang
 * @date 2022/6/22 15:32
 **/
@Service
@RequiredArgsConstructor
public class JobApiProvider implements JobApi {

    private final JobConfigBizService jobConfigBizService;


    @Override
    public PageList<JobVO> page(JobPageParamRpcRequest jobPageParamRpcRequest) {
        List<JobConfigDO> page = jobConfigBizService.page(jobPageParamRpcRequest);
        return PageList.convert(page, JobVO.class);
    }

    @Override
    public boolean count(String beanName, String cron) {
        return jobConfigBizService.count(beanName, cron, null);
    }

    @Override
    public boolean count(String beanName, String cron, String excludeId) {
        return jobConfigBizService.count(beanName, cron,excludeId);
    }

    @Override
    public String save(JobAddRpcRequest jobAddRpcRequest) {
        JobAddRequest jobAddRequest = BeanUtil.copyProperties(jobAddRpcRequest, JobAddRequest.class);
        return  jobConfigBizService.add(jobAddRequest);
    }

    @Override
    public boolean updateById(JobUpdateByIdRpcRequest jobUpdateByIdRpcRequest) {
        JobUpdateByIdRequest jobAddRequest = BeanUtil.copyProperties(jobUpdateByIdRpcRequest, JobUpdateByIdRequest.class);
        return jobConfigBizService.updateById(jobAddRequest);
    }

    @Override
    public boolean removeByIds(List<String> devJobIdList) {
        for (String s : devJobIdList) {
            jobConfigBizService.remoteById(s);
        }

        return true;
    }

    @Override
    public JobVO getById(String id) {
        JobGetByIdRequest jobGetByIdRequest = new JobGetByIdRequest(id);
        JobConfigDO byId = jobConfigBizService.getById(jobGetByIdRequest);
        return BeanUtil.copyProperties(byId,JobVO.class);
    }

    @Override
    public boolean stopJob(String id) {
        return jobConfigBizService.pause(id);
    }

    @Override
    public boolean runJob(String id) {
        return jobConfigBizService.run(id);
    }
}
